Euler Problem 68

See https://projecteuler.net/problem=68 for problem description.


In [1]:
from itertools import permutations

maxstring = ''
for v in permutations(range(1, 10)):
    v += (10,)
    magic = v[0] + v[4] + v[5]
    if all(v[k] + v[k+1] + v[k+6] == magic for k in range(4)):
        idx = min((v, i) for i, v in enumerate(v[5:]))[1]
        ring = [v[5], v[4], v[0], v[6], v[0], v[1], v[7], v[1], v[2], v[8], v[2], v[3], v[9], v[3], v[4]]
        ring = ring[3*idx:] + ring[:3*idx]
        string = ''.join(map(str, ring))
        if string > maxstring:
            maxstring = string
print(maxstring)


6531031914842725

In [ ]: